package com.cci.kangdao.service.impl;

import com.cci.kangdao.controller.ServiceStation;
import com.cci.kangdao.dao.CompanyTDao;
import com.cci.kangdao.dao.RolesTDao;
import com.cci.kangdao.dao.ScanRecordTDao;
import com.cci.kangdao.dao.ServiceStationDao;
import com.cci.kangdao.dao.UserGroupSubTDao;
import com.cci.kangdao.dao.UserGroupTDao;
import com.cci.kangdao.dao.UserTDao;
import com.cci.kangdao.dao.model.CompanyT;
import com.cci.kangdao.dao.model.RolesT;
import com.cci.kangdao.dao.model.ScanRecordT;
import com.cci.kangdao.dao.model.ServiceStationT;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.dao.model.UsergroupT;
import com.cci.kangdao.service.DictionariesTService;
import com.cci.kangdao.service.ScanRecordService;
import com.cci.kangdao.utilTool.CollectionUtils;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import com.cci.kangdao.utilTool.NumberUtils;
import com.cci.kangdao.utilTool.PublicTool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author William Du
 * @date 2019/12/2
 */
@Service
public class ScanRecordServiceImpl implements ScanRecordService {

    @Resource
    private UserTDao userTDao;

    @Resource
    private ScanRecordTDao scanRecordTDao;

    @Resource
    private RolesTDao rolesTDao;

    @Resource
    private DictionariesTService dictionariesTService;

    @Resource
    private ServiceStationDao serviceStationDao;

    @Resource
    private CompanyTDao companyTDao;

    @Resource
    private UserGroupTDao userGroupTDao;

    @Resource
    private UserGroupSubTDao userGroupSubTDao;




    @Override
    public void addRecord(Integer type ,Long userId, Long preRolesId, Long nowRolesId, Long operatorId) {
        ScanRecordT scanRecordT = new ScanRecordT();
        // 操作人信息
        UserT operator = userTDao.selectById(userId);
        scanRecordT.setFromUserId(operator.getId());
        scanRecordT.setFromUsername(operator.getUserName());
        // 被修改用户信息
        UserT userT = userTDao.selectById(userId);
        scanRecordT.setFromUserId(userT.getId());
        scanRecordT.setFromUsername(userT.getUserName());



        scanRecordT.setOperType(type);
        scanRecordTDao.insertSelective(scanRecordT);
    }

    @Override
    public void addRecord(Integer type, Long operatorId, Long userId, String preRoleNames, String nowRoleNames) {
        // 操作人信息
        UserT operator = userTDao.selectById(userId);
        // 被修改用户信息
        UserT userT = userTDao.selectById(userId);
        // 插入记录
        addRecord(type,operator,userT,preRoleNames,nowRoleNames);
    }

    @Override
    public void addRecord(Integer type, UserT operator, UserT userT, String preRoleNames, String nowRoleNames) {
        ScanRecordT scanRecordT = new ScanRecordT();
        if(null != operator && null != userT){
            // 操作人信息
            scanRecordT.setFromUserId(operator.getId());
            scanRecordT.setFromUsername(operator.getUserName());

            // 被修改用户信息
            scanRecordT.setUserId(userT.getId());
            scanRecordT.setUsername(userT.getUserName());

            scanRecordT.setRolesPre(preRoleNames);
            scanRecordT.setRolesNow(nowRoleNames);

            scanRecordT.setUserGroupId(operator.getUsergroupId());
            if(NumberUtils.isPositiveNum(operator.getUsergroupId())){
                scanRecordT.setUserGroupName(userGroupTDao.getUserGroupNameById(operator.getUsergroupId()));
            }

            scanRecordT.setSubUserGroupId(operator.getUsergroupSubId());
            if(NumberUtils.isPositiveNum(operator.getUsergroupSubId())){
                scanRecordT.setSubUserGroupName(userGroupSubTDao.getSubUserGroupNameById(operator.getUsergroupSubId()));
            }

            // 企业信息
            scanRecordT.setCompanyId(operator.getCompanyId());
            if(NumberUtils.isPositiveNum(operator.getCompanyId())){
                CompanyT companyT = companyTDao.getCompanyInfoByCompanyID(operator.getCompanyId());
                scanRecordT.setCompanyName(companyT.getCompanyName());
            }
            // 服务商信息
            scanRecordT.setServiceStationId(operator.getServiceStationID());
            if(NumberUtils.isPositiveNum(operator.getServiceStationID())){
                ServiceStationT serviceStation = serviceStationDao.findServiceStationByID(String.valueOf(operator.getServiceStationID()));
                scanRecordT.setServiceStationName(serviceStation.getStationname());
            }
            // dealerLevel
            scanRecordT.setDealerLevel(operator.getDealerLevel());
            scanRecordT.setDealerLevelCn(dictionariesTService.getDictionaryDValueByDInt("dealerLevel",operator.getDealerLevel()));

            scanRecordT.setPosition(operator.getPosition());
            scanRecordT.setOperType(type);
            scanRecordT.setSystime(LocalTimeUtil.getCurrentTime());
            scanRecordT.setFlag(0);
            scanRecordTDao.insertSelective(scanRecordT);
        }
    }

    @Override
    public String getUserRoles(Long userId) {
        List<RolesT> rolesTList = rolesTDao.getRoleListByUserID(userId);
        StringBuffer sb = new StringBuffer();
        if(CollectionUtils.isNotEmpty(rolesTList)){
            for(RolesT rolesT:rolesTList){
                sb.append(rolesT.getRolesName()).append(",");
            }
            return sb.substring(0,sb.length()-1);
        }
        return sb.toString();
    }
}
